package edu.scau.misp.backend_module.service.impl;

import edu.scau.misp.backend_module.core.rms_dto.OrderDTO;
import edu.scau.misp.backend_module.domain.*;
import edu.scau.misp.backend_module.mapper.*;
import edu.scau.misp.backend_module.service.IOrderDTOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Service
public class OrderDTOServiceImpl implements IOrderDTOService {

    private OrderDTO currentOrder;
    @Autowired
    private OrderTableMapper orderTableMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private TableMapper tableMapper;
    @Autowired
    private OrderDealMapper orderDealMapper;
    @Autowired
    private OrderDishAdditionMapper orderDishAdditionMapper;

    /**
     * 开始新的订单
     */
    @Override
    public OrderDTO makeNewOrder() {
        currentOrder = new OrderDTO();
        currentOrder.setOrderDate(new Date());

//        currentOrder.setTableId();
        currentOrder.setTotalPrice(0L);



        return currentOrder;
    }

    /**
     * 实例化SaleItem
     *
     * @param dishAddition 商品对象
     * @param quantity     数量
     */
    @Override
    public void makeLineItemDishAddition(DishAddition dishAddition, int quantity) {
        if (!isEntered(dishAddition.getDishAdditionSn(),quantity)){

        }


//        if(!isEntered(product.getProductSn(),quantity)){
//            SaleItem saleItem = new SaleItem();
//            saleItem.setProductDescription(product);
//            saleItem.setQuantity(quantity);
//            saleItem.setProductId(product.getProductId());
//            saleItem.setPrice(product.getPrice());
////            saleItem.setCreateTime(new Date());
//            saleItem.setStatus("0");
//            currentSale.getSaleItems().add(saleItem);
        }





    /**
     * 实例化SaleItem
     *
     * @param deal     商品对象
     * @param quantity 数量
     */
    @Override
    public void makeLineItemDeal(Deal deal, int quantity) {

    }

//    /**
//     * 实例化SaleItem
//     *
//     * @param orderItem 商品对象
//     * @param quantity  数量
//     */
//    @Override
//    public void makeLineItem(OrderItem orderItem, int quantity) {
//
//    }

    /**
     * 结束订单商品录入
     *
     * @return 总金额
     */
    @Override
    public BigDecimal orderEnd() {
        return null;
    }

    /**
     * 订单支付
     *
     * @param cashTendered 付款金额
     * @return 找零
     */
    @Override
    public BigDecimal makePayment(BigDecimal cashTendered) {
        return null;
    }

    /**
     * @param itemSn
     * @param quantity
     */
    @Override
    public void changeQuantityOfOrderItem(String itemSn, int quantity) {

    }

    /**
     * @param itemSn
     */
    @Override
    public void deleteOrderItemBySn(String itemSn) {

    }

    /**
     * @param orderId
     * @return
     */
    @Override
    public OrderDTO getOrderDTOById(Long orderId) {

        Order order = orderMapper.getOrderById(orderId);
        OrderDTO orderDTO = new OrderDTO(order);

        OrderTable orderTable = new OrderTable();
        orderTable.setOrderId(orderId);
        Long tableId =  orderTableMapper.selectOrderTableList(orderTable).get(0).getTableId();
        System.out.println(tableId);
        orderDTO.setTableId(tableId);
        orderDTO.setTable(tableMapper.getTableById(tableId));

        OrderDishAddition param1 = new OrderDishAddition();
        param1.setOrderId(orderId);
        OrderDeal param2 = new OrderDeal();
       // param2.setOrderId(orderId);

        List<OrderDishAddition> orderDishAdditions= orderDishAdditionMapper.selectOrderDishAdditionList(param1);
        List<OrderDeal> orderDeals = orderDealMapper.selectOrderDealList(param2);

        orderDTO.addOrderDishAdditions(orderDishAdditions);
        orderDTO.addOrderDeals(orderDeals);

        orderDTO.calculateTotalPrice();


        orderDTO.formatOrderList();

        return orderDTO;
    }

    /**
     * @return
     */
    @Override
    public List<OrderDTO> listAllOrderDTO() {
        return null;
    }

    /**
     * @param orderDTO
     * @return
     */
    @Override
    public List<OrderDTO> selectOrderDTOList(OrderDTO orderDTO) {
        return null;
    }

    /**
     * @param orderDTO
     * @return
     */
    @Override
    public int insertOrderDTO(OrderDTO orderDTO) {
//        Order order = new Order();
//        order.setOrderId(orderDTO.getOrderId());
//        order.setOrderDate(orderDTO.getOrderDate());
//        order.setOrderRemark(orderDTO.getOrderRemark());
//        order.setTotalPrice(order.getTotalPrice());
//
//        OrderTable orderTable = new OrderTable();
//        orderTable.setTableId(orderDTO.getTableId());
//        orderTable.setOrderId(orderDTO.getOrderId());
//        int status = 0;
//        status = status + orderTableMapper.insertOrderTable(orderTable);
//
//
//
//
//
//
//
//        return status;
        return  0;
    }

    /**
     * @param orderDTO
     * @return
     */
    @Override
    public int updateOrderDTO(OrderDTO orderDTO) {
        return 0;
    }

    /**
     * @param orderId
     * @return
     */
    @Override
    public int deleteOrderDTOById(Long orderId) {
        return 0;
    }
    /**
     * 判断商品是否已录入
     * @param itemSn
     * @param quantity
     * @return
     */
    private boolean isEntered(Long itemSn,int quantity){
        boolean flag = false;
        for(OrderItem si : currentOrder.getList()){
            // 如果已经录入则修改数量

        }
        return flag;
    }
}

